package com.ef.web.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.Number;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;

import com.ef.constant.EClassType;
import com.ef.dao.LeadsClassEntityDao;
import com.ef.util.CalendarUtil;
import com.ef.web.domain.TodayCourseCount;

public class StudentsByCourseAction extends ReportAction {

	private static final long serialVersionUID = 1L;

	private Date today;

	private int year;

	private int week;

	private TodayCourseCount tcc;

	private LeadsClassEntityDao leadsClassEntityDao;

	private List<TodayCourseCount> wkCount;
	private List<TodayCourseCount> lcCount;
	private List<TodayCourseCount> f2fCount;

	public StudentsByCourseAction() {
		Calendar now = Calendar.getInstance();
		now.setFirstDayOfWeek(Calendar.MONDAY);
		int day = now.get(Calendar.DAY_OF_YEAR);
		week = now.get(Calendar.WEEK_OF_YEAR);
		year = now.get(Calendar.YEAR);
		if (day > 7 && week == 1) {
			year++;
		}
		// NOTE week要-1
		if (year == 2010) {
			week = week - 1;
		}
	}

	public String list() {
		// if (today == null)
		// today = new Date();
		//
		// tcc = leadsClassEntityDao.countTodayCourseInfo(today);
		// NOTE week要-1
		int trueWeek=week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		List<Date> normal = CalendarUtil.getNormalDaysWithMonDayIsFirstDay(
				year, trueWeek);
		List<Date> weekend = CalendarUtil.getWeekendDaysWithMonDayIsFirstDay(
				year, trueWeek);

		List<TodayCourseCount> wk = new ArrayList<TodayCourseCount>();
		List<TodayCourseCount> lc = new ArrayList<TodayCourseCount>();
		List<TodayCourseCount> f2f = new ArrayList<TodayCourseCount>();
		for (Date t : normal) {
			TodayCourseCount wktcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.WK);
			wk.add(wktcc);
			TodayCourseCount lctcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.LC);
			lc.add(lctcc);
			TodayCourseCount f2ftcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.F2F);
			f2f.add(f2ftcc);
		}
		for (Date t : weekend) {
			TodayCourseCount wktcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.WK);
			wk.add(wktcc);
			TodayCourseCount lctcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.LC);
			lc.add(lctcc);
			TodayCourseCount f2ftcc = leadsClassEntityDao
					.countTodaySpecialCourseInfo(t, EClassType.F2F);
			f2f.add(f2ftcc);
		}

		wkCount = wk;
		lcCount = lc;
		f2fCount = f2f;

		return LIST;
	}

	public String export() {
		String fn="周统计报表["+year+"_"+week+"].xls";
		list();
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(fn).getBytes(
							"gb2312"), "iso8859-1"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		File file = new File(ServletActionContext.getServletContext()
				.getRealPath("/temp"));
		if (!file.exists()) {
			file.mkdir();
		}
		WritableWorkbook workBook;
		try {
			workBook = Workbook.createWorkbook(new File(ServletActionContext
					.getServletContext().getRealPath("/temp/export.xls")));
			workBook.setProtected(false);
			WritableSheet wst = workBook.createSheet("export", 0);
			String title1 = "总提供课量（节）";
			String title2 = "订课满员数量（节）";
			String title3 = "等待学员数量（人）";
			String title4 = "实际到课学员数量（人）";
			String title5 = "总订课人数（人数）";
			String title6 = "等待上的数量（人数）";
//			wst.addCell(new Label(0, 0, title1));
//			wst.addCell(new Label(0, 1, title2));
//			wst.addCell(new Label(0, 2, title3));
//			wst.addCell(new Label(0, 3, title4));
//
//			wst.addCell(new Label(1, 0, String.valueOf(tcc
//					.getAvailableCourseCount())));
//			wst.addCell(new Label(1, 1, String
//					.valueOf(tcc.getFullCourseCount())));
//			wst.addCell(new Label(1, 2, String
//					.valueOf(tcc.getWaitingStudents())));
//			wst.addCell(new Label(1, 3, String
//					.valueOf(tcc.getCheckinStudents())));

			WritableFont font_content = new WritableFont(WritableFont.TIMES, 10);
			WritableCellFormat contentStyle = new WritableCellFormat(
					font_content);
			contentStyle.setAlignment(Alignment.CENTRE);
			contentStyle.setBorder(Border.ALL, BorderLineStyle.THIN);
			contentStyle.setWrap(false);
			// F2F
			wst.addCell(new Label(0, 1, "F2F"));
			wst
					.addCell(new Label(0, 2, "week" + year + "" + week,
							contentStyle));
			wst.addCell(new Label(1, 2, title1, contentStyle));
			wst.addCell(new Label(2, 2, title5, contentStyle));
			wst.addCell(new Label(3, 2, title2, contentStyle));
			wst.addCell(new Label(4, 2, title3, contentStyle));
			wst.addCell(new Label(5, 2, title6, contentStyle));
			wst.addCell(new Label(6, 2, title4, contentStyle));

			for (int i = 0; i < f2fCount.size(); i++) {
				TodayCourseCount tmp = f2fCount.get(i);
				wst.addCell(new Label(0, 3 + i, tmp.getDispalyName(),contentStyle));
				wst.addCell(new Number(1, 3 + i, tmp.getAvailableCourseCount(), contentStyle));
				wst.addCell(new Number(2, 3 + i, tmp.getAllBookingStudents(), contentStyle));
				wst.addCell(new Number(3, 3 + i, tmp.getFullCourseCount(), contentStyle));
				wst.addCell(new Number(4, 3 + i, tmp.getWaitingStudents(), contentStyle));
				wst.addCell(new Number(5, 3 + i, tmp.getWaitingForClassStudents(), contentStyle));
				wst.addCell(new Number(6, 3 + i, tmp.getCheckinStudents(), contentStyle));
			}

			// WK
			wst.addCell(new Label(0, 11, "Workshop"));
			wst.addCell(new Label(0, 12, "week" + year + "" + week,
					contentStyle));
			wst.addCell(new Label(1, 12, title1, contentStyle));
			wst.addCell(new Label(2, 12, title5, contentStyle));
			wst.addCell(new Label(3, 12, title2, contentStyle));
			wst.addCell(new Label(4, 12, title3, contentStyle));
			wst.addCell(new Label(5, 12, title6, contentStyle));
			wst.addCell(new Label(6, 12, title4, contentStyle));

			for (int i = 0; i < wkCount.size(); i++) {
				TodayCourseCount tmp = wkCount.get(i);
				wst.addCell(new Label(0, 13 + i, tmp.getDispalyName(),
						contentStyle));
				wst.addCell(new Number(1, 13 + i, tmp.getAvailableCourseCount(), contentStyle));
				wst.addCell(new Number(2, 13 + i,tmp.getAllBookingStudents(), contentStyle));
				wst.addCell(new Number(3, 13 + i,tmp.getFullCourseCount(), contentStyle));
				wst.addCell(new Number(4, 13 + i, tmp.getWaitingStudents(), contentStyle));
				wst.addCell(new Number(5, 13 + i, tmp.getWaitingForClassStudents(), contentStyle));
				wst.addCell(new Number(6, 13 + i, tmp.getCheckinStudents(), contentStyle));
			}

			// LC
			wst.addCell(new Label(0, 21, "Life Club"));
			wst.addCell(new Label(0, 22, "week" + year + "" + week,
					contentStyle));
			wst.addCell(new Label(1, 22, title1, contentStyle));
			wst.addCell(new Label(2, 22, title5, contentStyle));
			wst.addCell(new Label(3, 22, title2, contentStyle));
			wst.addCell(new Label(4, 22, title3, contentStyle));
			wst.addCell(new Label(5, 22, title6, contentStyle));
			wst.addCell(new Label(6, 22, title4, contentStyle));

			for (int i = 0; i < lcCount.size(); i++) {
				TodayCourseCount tmp = lcCount.get(i);
				wst.addCell(new Label(0, 23 + i, tmp.getDispalyName(),contentStyle));
				wst.addCell(new Number(1, 23 + i, tmp.getAvailableCourseCount(), contentStyle));
				wst.addCell(new Number(2, 23 + i, tmp.getAllBookingStudents(), contentStyle));
				wst.addCell(new Number(3, 23 + i, tmp.getFullCourseCount(), contentStyle));
				wst.addCell(new Number(4, 23 + i, tmp.getWaitingStudents(), contentStyle));
				wst.addCell(new Number(5, 23 + i, tmp.getWaitingForClassStudents(), contentStyle));
				wst.addCell(new Number(6, 23 + i, tmp.getCheckinStudents(), contentStyle));
			}

			workBook.write();
			workBook.close();
			RequestDispatcher view = getRequest().getRequestDispatcher(
					"../../temp/export.xls");
			try {
				view.forward(getRequest(), getResponse());
			} catch (ServletException e) {

				e.printStackTrace();
			}

		} catch (IOException e) {

			e.printStackTrace();
		} catch (RowsExceededException e) {

			e.printStackTrace();
		} catch (WriteException e) {

			e.printStackTrace();
		}

		return NONE;
	}

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getWeek() {
		return week;
	}

	public void setWeek(int week) {
		this.week = week;
	}

	public LeadsClassEntityDao getLeadsClassEntityDao() {
		return leadsClassEntityDao;
	}

	public void setLeadsClassEntityDao(LeadsClassEntityDao leadsClassEntityDao) {
		this.leadsClassEntityDao = leadsClassEntityDao;
	}

	public TodayCourseCount getTcc() {
		return tcc;
	}

	public void setTcc(TodayCourseCount tcc) {
		this.tcc = tcc;
	}

	public List<TodayCourseCount> getWkCount() {
		return wkCount;
	}

	public void setWkCount(List<TodayCourseCount> wkCount) {
		this.wkCount = wkCount;
	}

	public List<TodayCourseCount> getLcCount() {
		return lcCount;
	}

	public void setLcCount(List<TodayCourseCount> lcCount) {
		this.lcCount = lcCount;
	}

	public List<TodayCourseCount> getF2fCount() {
		return f2fCount;
	}

	public void setF2fCount(List<TodayCourseCount> f2fCount) {
		this.f2fCount = f2fCount;
	}

}
